package dao.impl;

import dao.GoodsDAO;
import pojo.Goods;

import java.util.List;

/**
 * @author CaiKe
 * @create 2021/1/8
 */
public class GoodsDAOImpl extends BaseDAO implements GoodsDAO {

    @Override
    public Integer addGoods(Goods goods) {
        String sql = "insert into t_goods(storeId,ownerId,name,code,num,type,shop,unit,color,model,texture,prePurchasing,preSelling,description,updateTime,img) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        return update(sql, goods.getStoreId(), goods.getOwnerId(), goods.getName(), goods.getCode(), goods.getNum(), goods.getType(), goods.getShop(), goods.getUnit(), goods.getColor(), goods.getModel(), goods.getTexture(), goods.getPrePurchasing(), goods.getPreSelling(), goods.getDescription(), goods.getUpdateTime(), goods.getImg());
    }

    @Override
    public Integer deleteGoodsById(int id) {
        String sql = "delete from t_goods where id = ?";
        return update(sql, id);
    }

    @Override
    public Integer portGoods(int id, int num) {
        String sql = "update t_goods set num = ? where id = ?";
        return update(sql, num, id);
    }

    @Override
    public Integer queryGoodsNumById(int id) {
        String sql = "select num from t_goods where id=?";
        Number num = (Number)queryForSingleValue(sql, id);
        return num.intValue();
    }

    @Override
    public Integer updateGoods(Goods goods) {
        String sql = "update t_goods set ownerId=?,StoreId=?,name=?,code=?," +
                "num=?,type=?,shop=?,unit=?,color=?,model=?,texture=?," +
                "prePurchasing=?,preSelling=?,description=?,updateTime=?,img=? where id=?";
        return update(sql, goods.getOwnerId(), goods.getStoreId(), goods.getName(), goods.getCode(),
                goods.getNum(), goods.getType(), goods.getShop(), goods.getUnit(),
                goods.getColor(), goods.getModel(), goods.getTexture(), goods.getPrePurchasing(),
                goods.getPreSelling(), goods.getDescription(), goods.getUpdateTime(), goods.getImg(),
                goods.getId());
    }

    @Override
    public Goods queryGoodsById(int id) {
        String sql = "select * from v_store_goods where id = ?";
        return queryForOne(Goods.class, sql, id);
    }

    @Override
    public List<Goods> queryAllGoods() {
        String sql = "select * from v_store_goods";
        return queryForList(Goods.class, sql);
    }

    @Override
    public List<Goods> queryAllGoods(int aimId, int chose) {
        String sql = null;
        if (chose == 1) {
            sql = "select * from v_store_goods where storeId = ?";
        } else if (chose == 0) {
            sql = "select * from v_store_goods where ownerId = ?";
        } else {
            System.err.println("@param chose not right!! please check input!!");
        }
        return queryForList(Goods.class, sql, aimId);
    }

    @Override
    public Integer queryForGoodsNum() {
        String sql = "select count(1) from t_goods";
        Number number = (Number) queryForSingleValue(sql);
        return number.intValue();
    }

    @Override
    public List<Goods> queryLatestGoods(int num) {
        String sql = "select * from v_store_goods order by updateTime desc limit 0,?";
        return queryForList(Goods.class, sql, num);
    }

    @Override
    public Integer deleteGoodsByStoreId(int storeId) {
        String sql = "delete from t_goods where storeId = ?";
        return update(sql, storeId);
    }

}